#include "appMainWnd.h"

#include <osgDB/ReadFile>
#include <osg/ShapeDrawable>
#include <osg/TexGen>
#include <osgGA/TerrainManipulator>

#include <skyBox>
#include <logger>
#include <ctl>

#include <QMessageBox>
#include <QTreeWidgetItem>
#include <QDebug>
#include <QFileInfo>

Osgis::AppMainWnd::AppMainWnd( QWidget *parent /*= 0*/ )
{
	setupUi(this);
	initWnd();
	_funcParentLst = new QList<QTreeWidgetItem*>();
	_viewerWidget = new ViewerWidget();
	_viewerWidget->setParent(this->frame3d);
	QGridLayout* layout = new QGridLayout;
	layout->setMargin(0);
	layout->addWidget( _viewerWidget);

	this->frame3d->setLayout(layout);

	_rootModel = new osg::Group;
	
	//

	loadSky();
	loadModel("data/yuelan.ive");
	//Osgis::TravelManipulator::TravelToScence(_viewerWidget->getViewer());
	_viewerWidget->getViewer()->setCameraManipulator(new osgGA::TerrainManipulator());
	_viewerWidget->setScene(_rootModel);
}

Osgis::AppMainWnd::~AppMainWnd()
{

}

void Osgis::AppMainWnd::setNavagationTitle(const QString& title)
{
	this->treeWidgetFunc->setHeaderLabel(title);
}

void Osgis::AppMainWnd::initWnd()
{
	QHeaderView* header = new QHeaderView(Qt::Horizontal);
	header->setClickable(false);
	header->setMovable(false);
	header->setResizeMode(QHeaderView::Fixed);  
	header->setDefaultSectionSize(200); 
	treeWidgetFunc->setHeader(header);
	
}

void Osgis::AppMainWnd::showMessage(const QString& title,  const QString& msg )
{
	QMessageBox::information(this, title, msg);
}

int Osgis::AppMainWnd::addNavagationGroup( const QString& groupName )
{
	QTreeWidgetItem *group = new QTreeWidgetItem(treeWidgetFunc);
	group->setText(0, groupName);
	_funcParentLst->append(group);
	return _funcParentLst->length() - 1;
}

void Osgis::AppMainWnd::addNavagationFunction( const QString& funcName, int groupId )
{
	QTreeWidgetItem* parent = _funcParentLst->at(groupId);
	QTreeWidgetItem *func = new QTreeWidgetItem(parent);
	func->setText(0, funcName);
}

void Osgis::AppMainWnd::addNavagationCheck( const QString& funcName, int groupId )
{
	QTreeWidgetItem* parent = _funcParentLst->at(groupId);
	QTreeWidgetItem *func = new QTreeWidgetItem(parent);
	func->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	func->setText(0, tr("River"));
	func->setCheckState(0, Qt::Unchecked);
	func->setText(0, funcName);
}

void Osgis::AppMainWnd::onNavagationCheck( QTreeWidgetItem * item, int column )
{
	qDebug()<<"---------------------";
	QString name = item->text(column);
	bool checked = item->checkState(column) == Qt::Checked;
	emit checkChanged(name, checked);
}

void Osgis::AppMainWnd::onNavagationDoubleClick( QTreeWidgetItem * item, int column )
{
	QString name = item->text(column);
	emit bottonClicked(name);
}

void Osgis::AppMainWnd::enableNavagation( bool enable )
{
	if(enable)
	{
		connect(treeWidgetFunc, SIGNAL(itemChanged (QTreeWidgetItem*, int)), this, SLOT(onNavagationCheck(QTreeWidgetItem*, int)));
		connect(treeWidgetFunc, SIGNAL(itemDoubleClicked (QTreeWidgetItem*, int)), this, SLOT(onNavagationDoubleClick(QTreeWidgetItem*, int)));
	}
	else
	{
		disconnect(treeWidgetFunc, SIGNAL(itemChanged (QTreeWidgetItem*, int)), this, SLOT(onNavagationCheck(QTreeWidgetItem*, int)));
		disconnect(treeWidgetFunc, SIGNAL(itemDoubleClicked (QTreeWidgetItem*, int)), this, SLOT(onNavagationCheck(QTreeWidgetItem*, int)));
	}
}

bool Osgis::AppMainWnd::loadModel( const QString& fileName )
{
	_EZLOG_INFO(1)<<"Loding model:"<<fileName.toStdString()<<"\n";
	QFileInfo f(fileName);
	for(std::list<NAMED_MODEL>::iterator it = _modelsLst.begin(); it != _modelsLst.end(); ++ it)
	{
		if((*it).model_name == f.baseName())
		{
			_EZLOG_INFO(1)<<"Find loaded model."<<"\n";
			return true;
		}
	}
	osg::ref_ptr<osg::Node> mod = osgDB::readNodeFile(fileName.toStdString());
	
	if(!mod)
	{
		_EZLOG_FATAL(1)<<"Fail to open model:"<<fileName.toStdString()<<"\n";
		return false;
	}
	NAMED_MODEL nm;
	nm.model_name = f.baseName();
	nm.model_Node = mod.get();
	_modelsLst.push_back(nm);
	if(_rootModel->addChild(mod))
		_EZLOG_INFO(1)<<"Model opened"<<"\n";
	return true;
}

bool Osgis::AppMainWnd::loadModel(const QString & name, osg::Node* model)
{
	_EZLOG_INFO(1)<<"Loding model:"<<name.toStdString()<<"\n";
	for(std::list<NAMED_MODEL>::iterator it = _modelsLst.begin(); it != _modelsLst.end(); ++ it)
	{
		if((*it).model_name == name)
		{
			_EZLOG_INFO(1)<<"Find loaded model."<<"\n";
			return true;
		}
	}

	NAMED_MODEL nm;
	nm.model_name = name;
	nm.model_Node = model;
	_modelsLst.push_back(nm);
	if(_rootModel->addChild(model))
		_EZLOG_INFO(1)<<"Model opened"<<"\n";
	return true;
}

bool Osgis::AppMainWnd::unLoadMode( const QString& fileName )
{
	return true;
}



bool Osgis::AppMainWnd::loadSky()
{
// 	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
// 	geode->addDrawable( new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(), 100000.0f)));
// 	geode->setCullingActive( false );
// 	osg::ref_ptr<Osgis::SkyBox> skybox = new Osgis::SkyBox();
// 	skybox->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::TexGen );
// 	skybox->setEnvironmentMap( 0,
// 		osgDB::readImageFile("data/Cubemap_snow/posx.jpg"), osgDB::readImageFile("data/Cubemap_snow/negx.jpg"),
// 		osgDB::readImageFile("data/Cubemap_snow/posy.jpg"), osgDB::readImageFile("data/Cubemap_snow/negy.jpg"),
// 		osgDB::readImageFile("data/Cubemap_snow/posz.jpg"), osgDB::readImageFile("data/Cubemap_snow/negz.jpg") );
// 	skybox->addChild( geode.get() );

	osg::ref_ptr<osg::Node> skybox = createSkyBoxCubeMap("data/rnl_cross.hdr");
	return loadModel("sky", skybox);
}

bool Osgis::AppMainWnd::test()
{
	std::string name;
	osg::ref_ptr<osg::Group> building = _engine.parse("config/layer.lua", name);;
	_rootModel->addChild(building);
	osg::ref_ptr<osg::Group> text = _engine.parse("config/text.lua", name);;
	_rootModel->addChild(text);
	
	return true;
}

